<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Storage;

use App\Models\Article;
use App\Models\Config;

class ArticleController extends ResourceController
{

   /*
    |--------------------------------------------------------------------------
    |  Article Controller
    |--------------------------------------------------------------------------
    |
    | 文章控制器，文章的CURD
    |
    */

    /**
     * beforeUpdate
     * 组合需要更新的数组
     *
     * @return \Illuminate\Http\Response
     */
    public function beforeUpdate($datas,$detail)
    {
        $returnData['status'] = true;
        $returnData['msg'] = 'success';


        //是否禁用
        $datas['active'] = isset($datas['active']) ? 1 : 0;
        //是否替换图片title
        $datas['imgreplace'] = isset($datas['imgreplace']) ? 1 : 0;
        //是否添加水印
        $datas['waterMakr'] = isset($datas['waterMakr']) ? 1 : 0;

        //图片处理，优先查出配置项，以防助手函数多次查询
        $configs = Config::whereIn('name', ['water_img', 'water_position', 'water_margin_x', 'water_margin_y'])->pluck('content', 'name')->toArray();

        //缩略图 是否替换
        if (isset($datas['imgpath']) && $datas['imgpath'] != $detail['imgpath'] && (Storage::exists($datas['imgpath']))) {
            Storage::move($datas['imgpath'], str_replace('temp','article/'.date('Ymd'),$datas['imgpath']));
            $datas['imgpath']=str_replace('temp','article/'.date('Ymd'),$datas['imgpath']);
            $datas['waterMakr'] && waterImg($datas['imgpath'], $configs);
        } else {
            $datas['imgpath']=$detail['imgpath'];
        }
        
        //内容图片设置
        $title = $datas['imgreplace'] ? $datas['name'] : '';
        $ueditorContentImgs = ueditorContentImgs($detail['content'], $datas['content'], $title, $datas['waterMakr'], $configs);
        $datas['content'] = $ueditorContentImgs['content'];
        unset($datas['imgreplace'], $datas['waterMakr']);

        $returnData['data'] = $datas;
        return $returnData;
    }

     /**
     * beforeStore
     * 组合需要更新的数组
     *
     * @return \Illuminate\Http\Response
     */
    public function beforeStore($datas)
    {
        $returnData['status'] = true;
        $returnData['msg'] = 'success';
        

        //是否禁用
        $datas['active'] = isset($datas['active']) ? 1 : 0;
        //是否替换图片title
        $datas['imgreplace'] = isset($datas['imgreplace']) ? 1 : 0;
        //是否添加水印
        $datas['waterMakr'] = isset($datas['waterMakr']) ? 1 : 0;
               
        //图片处理，优先查出配置项，以防助手函数多次查询
       $configs = Config::whereIn('name', ['water_img', 'water_position', 'water_margin_x', 'water_margin_y'])->pluck('content', 'name')->toArray();
       //缩略图 是否替换
        if (isset($datas['imgpath'])&& (Storage::exists($datas['imgpath']))) {
            Storage::move($datas['imgpath'], str_replace('temp','article/'.date('Ymd'),$datas['imgpath']));
            $datas['imgpath']=str_replace('temp','article/'.date('Ymd'),$datas['imgpath']);
            $datas['waterMakr'] && waterImg($datas['imgpath'], $configs);
        }

        //内容图片设置
        $title = $datas['imgreplace'] ? $datas['name'] : '';
        $ueditorContentImgs = ueditorContentImgs('', $datas['content'], $title, $datas['waterMakr'], $configs);
        $datas['content'] = $ueditorContentImgs['content'];
        unset($datas['imgreplace'], $datas['waterMakr']);

        $returnData['data'] = $datas;
        return $returnData;
    }

    /**
     * afterUpdate
     * 后置操作，如果修改了图片，删除之前的
     *
     * @return \Illuminate\Http\Response
     */
    public function afterUpdate($returnData,$detail)
    {
        $request = $this->getFormRequest();
        //删除替换之前的图片
        $imgpath = $request->input('imgpath','');
        if($returnData['status']&&$imgpath != $detail['imgpath']&&Storage::exists($detail['imgpath'])){
            Storage::delete($detail['imgpath']);
        }

        //删除替换之前的图片
        $content = $request->input('content', '');
        delUeditorImgs($detail['content'], $content);

        return $returnData;
    }

    
    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function afterDelete($result,$detail)
    {
        //
        if ($result) {
            delUeditorImgs($detail['content'],'');
            Storage::exists($detail['imgpath']) && Storage::delete($detail['imgpath']);
        }
        return true;
    }

    /**
     * formatData
     *
     * @return \Illuminate\Http\Response
     */
    public function formatData($data)
    {
        //
        foreach ($data as $key => $value) {
            $data[$key]['created_date'] = date('Y-m-d',$value['created_at']->timestamp);
            $data[$key]['updated_date'] = date('Y-m-d',$value['updated_at']->timestamp);
        }
        return $data;
    }
}
